package com.jming.jike.controller.backend;

import com.jming.jike.common.PageResult;
import com.jming.jike.common.ResponseResult;
import com.jming.jike.common.ValidatorGroup.Insert;
import com.jming.jike.common.ValidatorGroup.Query;
import com.jming.jike.common.ValidatorGroup.Update;
import com.jming.jike.dto.backend.EmployeeDTO;
import com.jming.jike.dto.backend.LoginDTO;
import com.jming.jike.dto.backend.UpdatePasswordDTO;
import com.jming.jike.entity.Employee;
import com.jming.jike.service.backend.EmployeeService;
import com.jming.jike.vo.backend.EmployeeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @author jinming.zhang
 * @version 0.0.1
 * @date 2022/04/24
 */
@RestController
@RequestMapping("/employee")
@Api(tags = "员工管理相关接口", value = "/employee")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    @Resource
    private HttpSession httpSession;


    @PostMapping("/insert")
    @ApiOperation(value = "添加员工信息", notes = "添加员工信息", produces = "application/json", httpMethod = "POST")
    public ResponseResult<Boolean> insert(@RequestBody @Validated(Insert.class) EmployeeDTO employeeDTO) {

        return ResponseResult.success(employeeService.insert(employeeDTO));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改员工信息", notes = "修改员工信息", produces = "application/json", httpMethod = "PUT")
    public ResponseResult<Boolean> update(@RequestBody @Validated(Update.class) EmployeeDTO employeeDTO) {

        return ResponseResult.success(employeeService.update(employeeDTO));
    }

    @PutMapping("/updateStatus/{id}/{status}")
    @ApiOperation(value = "禁/启用员工", notes = "禁/启用员工", produces = "application/json", httpMethod = "PUT")
    public ResponseResult<Boolean> updateStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {

        return ResponseResult.success(employeeService.updateStatus(id, status));
    }

    @PostMapping("/pageQuery")
    @ApiOperation(value = "分页查询员工信息", notes = "分页查询员工信息", produces = "application/json", httpMethod = "POST")
    public ResponseResult<PageResult<EmployeeVO>> pageQuery(@RequestBody @Validated(Query.class) EmployeeDTO employeeDTO) {

        return ResponseResult.success(employeeService.pageQuery(employeeDTO));
    }

    @PutMapping("/updatePassword")
    @ApiOperation(value = "修改密码", notes = "修改密码", produces = "application/json", httpMethod = "PUT")
    public ResponseResult<Boolean> updatePassword(@RequestBody @Validated(Update.class) UpdatePasswordDTO updatePasswordDTO) {

        return ResponseResult.success(employeeService.updatePassword(updatePasswordDTO));
    }

    @PostMapping("/login")
    @ApiOperation(value = "员工登录", notes = "员工登录", produces = "application/json", httpMethod = "POST")
    public ResponseResult<Boolean> login(@RequestBody @Validated(Query.class) LoginDTO loginDTO) {

        Employee employee = employeeService.login(loginDTO);
        // 将登录信息放入session中
        httpSession.setAttribute("Employee", employee);
        return ResponseResult.success(true);
    }

    @PostMapping("/logout")
    @ApiOperation(value = "员工退出", notes = "员工退出", produces = "application/json", httpMethod = "POST")
    public ResponseResult<String> logout(){

        //清理Session中保存的当前登录信息
        httpSession.removeAttribute("Employee");
        return ResponseResult.success("退出成功");
    }

}
